package com.server.thisService.common;


import com.server.thisService.common.enums.ExceptionCode;
import com.project.thisCommon.annotation.MethodAuthority;
import com.project.thisCommon.model.ResultBase;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class FunctionAop {

    @Pointcut("@annotation(com.project.thisCommon.annotation.MethodAuthority)")
    public void checkAuthority() {
    }

//    private static final String[] ALLOW_HEADS = {"app-id", "serial-number", "request-time", "device-id", "app-version", "market", "sign", "customer-id", "access-token", "os-version", "phone-models", "app-stage", "X-Token", "content-type"};

    @Around("checkAuthority() && @annotation(methodAuthority)")
    public Object Interceptor(ProceedingJoinPoint pjp, MethodAuthority methodAuthority) throws Throwable {
        Object rtn = null;
        try {
            rtn = pjp.proceed();
        } catch (Exception e){
            if (e instanceof ExceptionCommon){
                ExceptionCommon ex = (ExceptionCommon) e;
                rtn = new ResultBase<>(
                        null,
                        ex.getMessage(),
                        ex.getCode());
            } else {
                rtn = new ResultBase<>(
                        null,
                        ExceptionCode.SYSTEM_ERROR.getMessage(),
                        ExceptionCode.SYSTEM_ERROR.getCode());
            }
            System.out.print(e.getMessage());
            e.printStackTrace();
        }

        return rtn;
    }

}
